package com.android.dialnowsms;

import java.net.URLDecoder;

import android.provider.BaseColumns;
import android.provider.Contacts;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.PeopleColumns;
import android.provider.Contacts.Phones;
import android.provider.Contacts.PhonesColumns;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Button;

public class SMSComposer extends Activity {
  public EditText msg;
  public TextView chars_count;
  public EditText number;
  public TextView log;
  public ProgressBar progress;
  public Context context;
  public String sent_sms;
  private static final int MENU_PREFS = 1;
  private static final int MENU_ABOUT = 2;
  public static final String PREFS_NAME = "preferences";
  protected static final int PICK_CONTACT = 0;

  static String[] PEOPLE_PROJECTION = new String[] {
    BaseColumns._ID, Contacts.People.PRIMARY_PHONE_ID,
    PhonesColumns.TYPE, PhonesColumns.NUMBER,
    PhonesColumns.LABEL, PeopleColumns.NAME, };

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    number = ((AutoCompleteTextView) findViewById(R.id.number));
    log = ((TextView) findViewById(R.id.log));
    msg = ((EditText) findViewById(R.id.entry));
    chars_count = ((TextView) findViewById(R.id.chars_count));
    msg.addTextChangedListener(new SMSTextWatcher(this));
    
    Intent intent = getIntent();
    if (intent.getData() != null) {
      String param = intent.getDataString().replace("sms:", "").replace(" ", "")
          .replace("(0)", "").replace("-","").replace(".", "").replace("(", "")
          .replace(")","");
      number.setText(URLDecoder.decode(param));
    }

    Button sendMessageButton = (Button) findViewById(R.id.ok);
    ImageButton getContactsButton = (ImageButton) findViewById(
        R.id.contacts_button);
    progress = (ProgressBar) findViewById(R.id.progressbar);
    
    sendMessageButton.setOnClickListener(new SMSSender(this));
    getContactsButton.setOnClickListener(new GetContactsListener());

    ContentResolver content = getContentResolver();
    Cursor cursor = content.query(Contacts.People.CONTENT_URI,
        PEOPLE_PROJECTION, null, null,
        Contacts.People.DEFAULT_SORT_ORDER);
    ContactListAdapter adapter = new ContactListAdapter(this, cursor);

    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(
        R.id.number);
    textView.setAdapter(adapter);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0, MENU_PREFS, Menu.NONE, "Preferences").setIcon(
        android.R.drawable.ic_menu_preferences);
    menu.add(1, MENU_ABOUT, Menu.NONE, "About").setIcon(
        android.R.drawable.ic_menu_info_details);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_PREFS:
      startActivity(new Intent(this, EditPreferences.class));
      break;

    case MENU_ABOUT:
      About about = new About();
      about.showAboutDialog(this);
      break;
    }
    return (super.onOptionsItemSelected(item));
  }

  private class GetContactsListener implements View.OnClickListener {
    public void onClick(View v) {
      startActivityForResult(new Intent(Intent.ACTION_PICK,
          Phones.CONTENT_URI), PICK_CONTACT);
    }
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
      Intent data) {
    if (requestCode == PICK_CONTACT) {
      if (resultCode == RESULT_OK) {
        Cursor cur = this.managedQuery(data.getData(),
            new String[] { PhonesColumns.NUMBER }, null,
            null, null);
        if (cur.moveToFirst()) {
          String targetNumber = cur.getString(cur.getColumnIndex(
              PhonesColumns.NUMBER));
          targetNumber = targetNumber.replace(" ", "").replace("(0)", "").
          replace("-","").replace(".", "").replace("(", "").replace(")","");
          number.setText(targetNumber);
        } else {
          number.setText("");
        }
      }
    }
  }
}